﻿using Admin.ApplicationLib.Services;
using Admin.CommonLib.Constants;
using Admin.CommonLib.Models.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swimj.AspNetCore.Mvc;

namespace Admin.WebApi.Controllers
{
    public class UserController : SecurityController
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="model">筛选用户条件对象实例</param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.User.QueryUsers)]
        [Authorize(nameof(ApiRoutes.User.QueryUsers))]
        public async Task<IActionResult> Get([FromQuery] UserSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var result = await _userService.QueryUsersByPageAsync(model);
                return Ok(result);
            }
            else
            {
                var result = await _userService.QueryUsersAsync(model);
                return Ok(result);
            }
        }

        /// <summary>
        /// 查询用户明细
        /// </summary>
        /// <param name="id">用户唯一标识</param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.User.GetUser)]
        [Authorize(nameof(ApiRoutes.User.GetUser))]
        // [Authorize(nameof(ApiRoutes.User.UpdateUser))]
        public async Task<IActionResult> Get(Guid id)
        {
            var result = await _userService.GetUserAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="model">用户对象实例</param>
        /// <returns></returns>
        [HttpPost(ApiRoutes.User.CreateUser)]
        [Authorize(nameof(ApiRoutes.User.CreateUser))]
        public async Task<IActionResult> Post(UserEditRequestDto model)
        {
            var result = await _userService.CreateUserAsync(model);
            return Ok(result);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="id">用户唯一标识</param>
        /// <param name="model">用户对象实例</param>
        /// <returns></returns>
        [HttpPut(ApiRoutes.User.UpdateUser)]
        [Authorize(nameof(ApiRoutes.User.UpdateUser))]
        public async Task<IActionResult> Put(Guid id, UserEditRequestDto model)
        {
            var result = await _userService.UpdateUserAsync(id, model);
            return Ok(result);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">用户唯一标识</param>
        /// <returns></returns>
        [HttpDelete(ApiRoutes.User.DeleteUser)]
        [Authorize(nameof(ApiRoutes.User.DeleteUser))]
        public async Task<IActionResult> Delete(Guid id)
        {
            var result = await _userService.DeleteUserAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// 导出用户列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.User.ExportUser)]
        [Authorize(nameof(ApiRoutes.User.ExportUser))]
        public async Task<IActionResult> Export([FromQuery] UserSearchRequestDto model)
        {
            var result = await _userService.ExportAsync(model);
            return CustomFile(result.Item1, result.Item2);
        }

        /// <summary>
        /// 导出用户下载模板
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.User.ExportTemplateUser)]
        //[Authorize(nameof(ApiRoutes.User.ImportUser))]
        public async Task<IActionResult> ExportTemplate()
        {
            var result = await _userService.ExportTemplateAsync();
            return CustomFile(result.Item1, result.Item2);
        }

        /// <summary>
        /// 导入用户
        /// </summary>
        /// <param name="importType">导入类型 1.全量 2.增量</param>
        /// <param name="file">用户文件</param>
        /// <returns></returns>
        [HttpPost(ApiRoutes.User.ImportUser)]
        //[Authorize(nameof(ApiRoutes.User.ImportUser))]
        public async Task<IActionResult> Import([FromQuery] int importType, IFormFile file)
        {
            var stream = file.OpenReadStream();

            byte[] bytes = new byte[stream.Length];
            var _ = await stream.ReadAsync(bytes, 0, bytes.Length);
            //设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);

            MemoryStream memoryStream = new MemoryStream(bytes);
            var result = await _userService.ImportAsync(importType, memoryStream);
            return Ok(result);
        }
    }
}